<?php
/*
 * 配餐状态信息编辑及展示接口
 * */
class Apiv1_CaterController extends Ctrl_Api{
	public function listAction(){
		$p = $_REQUEST;
		$tCMOD = new CarteModel;
		$tCIMO = new CaterinfoModel;
		$pDepartment = empty($p['department'])?'':Tool_Fnc::safe_string($p['department']);
		$time = date('Y-m-d',time());
		$tSql = 'select catertime,ci.id,breakfast,lunch,dinner,other,breakfast_picrute,lunch_picrute,dinner_picrute,other_picture from '.$tCIMO->table.' ci left join '.$tCMOD->table.' c on ci.id = c.id where catertime="'.$time.'" and ci.status='.$pDepartment;
		$tDatas = $tCIMO->query($tSql);
		// $url = "http://fzzj.huayan360.com/upload./senior/2017/03/07/";
		if(!empty($tDatas)){
			$url =Yaf_Registry::get("config")->web->url->img. '/upload/cater/'.date('Y',strtotime($tDatas[0]['catertime'])).'/'.date('m',strtotime($tDatas[0]['catertime'])).'/'.date('d',strtotime($tDatas[0]['catertime'])).'/';
			$breakfast = array_filter(explode(',',$tDatas[0]['breakfast_picrute']));
			$lunch = array_filter(explode(',',$tDatas[0]['lunch_picrute']));
			$dinner = array_filter(explode(',',$tDatas[0]['dinner_picrute']));
			$other = array_filter(explode(',',$tDatas[0]['other_picture']));
			foreach ($breakfast as $key => $val) {$breakfast_picture[$key]['photo'] = $url.$val;}
			foreach ($lunch as $key => $val)  {$lunch_picture[$key]['photo'] = $url.$val;}
			foreach ($dinner as $key => $val) {$dinner_picture[$key]['photo'] = $url.$val;}
			foreach ($other as $key => $val)  {$other_picture[$key]['photo'] = $url.$val;}
			foreach ($tDatas as $key => $tData) {
				$tIDatas['id'] = $tData['id'];
				$tIDatas['breakfast']['cater'] = $tData['breakfast'];
				$tIDatas['lunch']['cater'] = $tData['lunch'];
				$tIDatas['dinner']['cater'] = $tData['dinner'];
				$tIDatas['other']['cater'] = $tData['other'];
				$tIDatas['breakfast']['picture'] = empty($breakfast_picture)?array():$breakfast_picture;
				$tIDatas['lunch']['picture'] = empty($lunch_picture)?array():$lunch_picture;
				$tIDatas['dinner']['picture'] = empty($dinner_picture)?array():$dinner_picture;
				$tIDatas['other']['picture'] = empty($other_picture)?array():$other_picture;
			}
		}else{
				$tIDatas['id']=" ";
				$tIDatas['breakfast']['cater'] = ' ';
				$tIDatas['lunch']['cater'] = ' ';
				$tIDatas['dinner']['cater'] = ' ';
				$tIDatas['other']['cater'] = ' ';
				$tIDatas['breakfast']['picture'] = array();
				$tIDatas['lunch']['picture'] = array();
				$tIDatas['dinner']['picture'] = array();
				$tIDatas['other']['picture'] = array();
			}
		Tool_Fnc::ajaxMsg('','1',$tIDatas);
	}
	public function editAction(){
		$q = $_REQUEST;
		$tCIMD = new CaterinfoModel;
		$tAMD = new AdminModel;
		$pCip = empty($q['ci_id'])?'':Tool_Fnc::safe_string($q['ci_id']);
		$pType = empty($q['type'])?'':Tool_Fnc::safe_string($q['type']);
		// $pDepartment = empty($q['department'])?'':Tool_Fnc::safe_string($q['department']);
		$department = $tAMD->field('department')->where('id='.$pCip)->fList();
		if (empty($_FILES['img']) || !count($_FILES['img'])) {Tool_Fnc::ajaxMsg('请上传配餐图片');}
		$tTime = date('Y-m-d',time());
		$tImgurl = '/upload/cater/' . date('Y'). '/' . date('m') . '/' . date('d') . '/';
		$tImgurl_small = '/upload/cater/' . date('Y'). '/' . date('m') . '/' . date('d') . '/small/';
		$tUpload = new Tool_Upload($_FILES['img'] , APPLICATION_PATH . '/public/'.  $tImgurl );
		$tSavename = $tUpload->getSaveName();
		$tRes = $tUpload->upload($tSavename);
		$tExt = $tUpload->extension;
		$tFile = $tSavename.'.'.$tExt;
		if ($pType==1) {
			$ziduan = 'breakfast_picrute';
		}elseif ($pType==2) {
			$ziduan = 'lunch_picrute';
		}elseif ($pType==3){
			$ziduan = 'dinner_picrute';
		}else{
			$ziduan = 'other_picture';
		}
		$tsql = 'select '.$ziduan.' from '.$tCIMD->table.' where catertime="'.$tTime.'" and status='.$department[0]['department'];
		$tDIDatas = $tCIMD->query($tsql);
		if (empty($tDIDatas[0][$ziduan])) {
			$picture = $tFile;
		}else{
			$picture = $tDIDatas[0][$ziduan].','.$tFile;
		}
		$sql = 'update '.$tCIMD->table.' SET '.$ziduan.'="'.$picture.'" WHERE status='.$department[0]['department'].' and catertime="'.$tTime.'"';
		if($tCIMD->query($sql)){
			Tool_Fnc::ajaxMsg('提交成功','1');die;
		}
		Tool_Fnc::ajaxMsg('提交成功','1');die;
	}
	public function nurseAction(){
		$p = $_REQUEST;
		$tWMD = new WorkerModel();
		$department = empty($p['department'])?'':Tool_Fnc::safe_string($p['department']);
		$Datas = $tWMD->field('id,name')->where('department='.$department)->fList();
		Tool_Fnc::ajaxMsg('','1',$Datas);die;
	}
	public function hlistAction(){
		$p = $_REQUEST;
		$tEMD = new ExamineModel;
		$tDIDatas = $tEMD->field('exam_title,exam_picture')->where('e_id=2')->fList();
		foreach ($tDIDatas as $key => $tDatas) {
			$Datas[$key]['type'] = $key;
			$Datas[$key]['content'] = $tDatas['exam_title']; 
			$Datas[$key]['photo'] = $tDatas['exam_picture'];
		}
		Tool_Fnc::ajaxMsg('','1',$Datas);
	}
	public function heditAction(){
		$p = $_REQUEST;
		$department = empty($p['department'])?'':Tool_Fnc::safe_string($p['department']);
		$status = empty($p['status'])?'':Tool_Fnc::safe_string($p['status']);
		if ($staus==1) {
			
		}
		die;
	}
	public function datelistAction(){
		$p = $_REQUEST;
		$Datetime = empty($p['datetime'])?'':Tool_Fnc::safe_string($p['datetime']);
		$department = empty($p['department'])?'':Tool_Fnc::safe_string($p['department']);
		$tCMOD = new CarteModel;
		$tCIMD = new CaterinfoModel;
		$tDCDatas = $tCIMD->field('breakfast,lunch,dinner,other')->where('status='.$department.' and catertime="'.$Datetime.'"')->fList();
		/*		if (empty($tDCDatas)) {
			$tDCDatas['breakfast'] = '暂无数据';
			$tDCDatas['lunch'] = '暂无数据';
			$tDCDatas['dinner'] = '暂无数据';
			$tDCDatas['other'] = '暂无数据';

			Tool_Fnc::ajaxMsg('','1',$tDCDatas);die;	
		}*/
		Tool_Fnc::ajaxMsg('','1',$tDCDatas);die;
	}
	//卫生推送接口
	public function pushAction(){
		$p = $_REQUEST;
		$tOMD = new OverseerModel;
		$time = date('Y-m-d',time());
		$department = empty($p['department'])?'':Tool_Fnc::safe_string($p['department']);
		$tSql = 'select max(id),status from '.$tOMD->table.' where department='.$department.' and date="'.$time.'"';
		$Odata = $tOMD->query($tSql);
		if (!empty($Odata)) {
			if ($Odata[0]['status']==1) {
				$PData = array('type'=>1,'content'=>'居室环境');
				Tool_Fnc::ajaxMsg('操作成功','1',$PData);die;
			}elseif ($Odata[0]['status']==2) {
				$PData = array('type'=>2,'content'=>'厨房环境');
				Tool_Fnc::ajaxMsg('操作成功','1',$PData);die;
			}elseif ($Odata[0]['status']==3) {
				$PData = array('type'=>3,'content'=>'卫生间环境');
				Tool_Fnc::ajaxMsg('操作成功','1',$PData);die;
			}
		}
		$PData = array();
		Tool_Fnc::ajaxMsg('操作成功','0',$PData);die;

	}
	//卫生推送反馈接口
	public function pullAction(){
		$p = $_REQUEST;
		$department = empty($p['department'])?'':Tool_Fnc::safe_string($p['department']);
		$tOMD = new OverseerModel;
		$time = date('Y-m-d',time());
		$tSql = 'select max(id),status from '.$tOMD->table.' where department='.$department.' and date="'.$time.'"';
		$Odata = $tOMD->query($tSql);
		if (!empty($Odata)) {
			$DATAS = array(
				'status' => 0,
				'id' => $Odata[0]["max(id)"],
				);
			if (!$tOMD->update($DATAS)) {
				Tool_Fnc::ajaxMsg('操作失败','0',$Odata);die;
			}
			Tool_Fnc::ajaxMsg('操作成功','1');die;
		}
		die;
	}
}
?> 